#!/bin/bash

############ ***************************************** ############
############ 以下参数需要在运行的时候配置， 可能需要进行修改 ############
############ ***************************************** ############

### 要测试的环境api调用地址
api_domain="codeup-proxy.aliyun.com"
### 要测试环境的api 调用 token
api_token="7LAHAHHdeBNbBg5rHSW0"

### clone的域名地址
clone_domain="daily-codeup.aliyun.com"
### 测试仓库所在企业
tenant_id="61234c2d1bd96aa110f27b9c"
### 测试仓库名称
REPO_NAME="test_trunk_base"
### 评审者本地clone仓库的目录名称
REVIEWER_REPO_NAME="test_trunk_base_for_reviewer"
REPO_DIR="$SHARNESS_TRASH_DIRECTORY"/"$REPO_NAME"
REVIEWER_REPO_DIR="$SHARNESS_TRASH_DIRECTORY"/"$REVIEWER_REPO_NAME"
### 仓库唯一的分支为master
BRANCH="master"
MULTI_BRANCH="dyrone/teng/aoming"

### 创建仓库的管理员用户名以及唯一ID
user="yunhuai"
user_external="61234ac87920cba1bd01d331"
user_http_name="xzy57369"
user_http_password="573609568"
user_id=18577
### 进行评审操作的用户名以及唯一ID
anotherUser="yunhuai.xzyFPr"
another_user_external="61370acf28a786064b0ff17d"
### https clone的用户名和密码
password="573609568"
another_userId=18675
READ_LEVEL=20
WRITE_LEVEL=30

init_repo() {
	git clone https://"$anotherUser":"$password"@"$clone_domain"/"$tenant_id"/$REPO_NAME.git "$REPO_DIR" &&
		git -C "$REPO_DIR" config user.name "Dyrone Teng" &&
		git -C "$REPO_DIR" config user.email "tenglong.tl@alibaba-inc.com"
}

remove_all_reviewers() {
	projectId=$1
	localId=$2
	curl -s --location --request POST "https://$api_domain/api/v4/projects/$projectId/code_reviews/$localId/person/reviewer" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Content-Type: application/json' \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1530712235839737' \
		--data-raw "{\"new_user_id_list\": []}"
}

add_reviewer() {
	projectId=$1
	localId=$2
	userId=$3
	curl -s --location --request POST "https://$api_domain/api/v4/projects/$projectId/code_reviews/$localId/person/reviewer" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Content-Type: application/json' \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1530712235839737' \
		--data-raw "{\"new_user_id_list\": [$userId]}"
}

add_member() {
	LEVEL=$1
	who=$2
	curl -s --location --request POST "https://$api_domain/api/v4/projects/$REPO_ID/members" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Content-Type: application/json' \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1530712235839737' \
		--data-raw "{\"extern_uids\": \"$who\", \"access_level\": $LEVEL}"
}


find_repo() {
	curl -s --location --request GET "http://$api_domain/api/v3/projects/find_with_namespace?path=$tenant_id%2F$REPO_NAME" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Content-Type: application/json' \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121'
}

create_repo() {
	curl -s --location --request POST "http://$api_domain/api/v3/projects" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Content-Type: application/json' \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121' \
		--data-raw "{\"visibility_level\":0,\"name\":\"$REPO_NAME\",\"path\":\"$REPO_NAME\",\"namespace_id\":110829,\"init_standard_service\":false,\"open_department_id_paths\":[]}" | jq .id
}

delete_repo() {
	curl --location --request POST "https://$api_domain/api/v3/projects/$REPO_ID/remove" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Content-Type: application/json' \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121' \
		--data-raw "{\"name\":\"$REPO_NAME\",\"reason\":\"1111\"}"
}

get_merge_requests() {
	curl -s --location --request GET "https://$api_domain/api/v4/merge_requests/advanced_search?_input_charset=utf-8&page=1&state=opened&search=&order_by=updated_at&project_ids=$REPO_ID" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Content-Type: application/json' \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121'
}

get_protected_branch() {
	curl -s --location --request GET "https://$api_domain/api/v4/projects/$REPO_ID/repository/protect_branches" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Content-Type: application/json' \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121'
}

protect_branch() {
	BRANCH=$1
	curl -s --location --request POST "https://$api_domain/api/v4/projects/$REPO_ID/repository/protect_branches" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Content-Type: application/json' \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121' \
		--data-raw "{\"branch\":\"$BRANCH\",\"allow_merge\":[40,30],\"allow_push\":[],\"test_names\":[],\"merge_request\":{\"required\":false,\"mr_mode\":\"general\",\"allow_self_approval\":false,\"minimum_approval\":1,\"default_assignees\":[],\"allow_merge_request\":[40,30]},\"test_setting\":{\"check_config\":{\"check_items\":[]}}}" >/dev/null
}

unprotect_branch() {
	br_id=$1
	curl -s --location --request DELETE "https://$api_domain/api/v4/projects/$REPO_ID/repository/protect_branches/$br_id" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Content-Type: application/json' \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121' >/dev/null
}

close_merge_request() {
	projectId=$1
	localId=$2
	curl -s --location --request POST "https://$api_domain/api/v4/projects/$projectId/code_reviews/$localId/close" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Content-Type: application/json' \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121'
}

enable_ref_review() {
	projectId=$1
	curl -s --location --request POST "https://$api_domain/api/v4/projects/$projectId/ref_review" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121' \
		--header 'Content-Type: application/json' \
		--data-raw "{\"enabled\":true}"
}

disable_ref_review() {
	projectId=$1
	curl -s --location --request POST "https://$api_domain/api/v4/projects/$projectId/ref_review" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121' \
		--header 'Content-Type: application/json' \
		--data-raw "{\"enabled\":false}"
}

update_branch_pattern() {
	projectId=$1
	pattern=$2
	curl -s --location --request PUT "https://$api_domain/api/v4/projects/$projectId/ref_review" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121' \
		--header 'Content-Type: application/json' \
		--data-raw "{\"branch_pattern\":\"$pattern\"}"
}

update_tag_pattern() {
	projectId=$1
	pattern=$2
	curl -s --location --request PUT "https://$api_domain/api/v4/projects/$projectId/ref_review" \
		--header "PRIVATE-TOKEN: $api_token" \
		--header "X-Tenant-Id: $tenant_id" \
		--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121' \
		--header 'Content-Type: application/json' \
		--data-raw "{\"tag_pattern\":\"$pattern\"}"
}

update_default_assignees() {
	projectId=$1
        who=$2
        curl -s --location --request PUT "https://$api_domain/api/v4/projects/$projectId/ref_review" \
        	--header "PRIVATE-TOKEN: $api_token" \
        	--header "X-Tenant-Id: $tenant_id" \
        	--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121' \
        	--header 'Content-Type: application/json' \
        	--data-raw "{\"default_assignees\":[\"$who\"]}"
}

query_ref_review() {
	projectId=$1
	crId=$2
        curl -s --location --request GET "https://$api_domain/api/v4/projects/$projectId/code_reviews/$crId" \
        	--header "PRIVATE-TOKEN: $api_token" \
        	--header "X-Tenant-Id: $tenant_id" \
        	--header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121' \
        	--header 'Content-Type: application/json'
}

execute_ref_review() {
	projectId=$1
        crId=$2
	curl -s --location --request POST "https://$api_domain/api/v4/projects/$projectId/code_reviews/$crId/merge" \
	        --header "PRIVATE-TOKEN: $api_token" \
                --header "X-Tenant-Id: $tenant_id" \
                --header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121' \
                --header 'Content-Type: application/json' \
                --data-raw "{\"merge_commit_message\": \"undefined\\n\\nundefined\",\"merge_type\": \"execute\"}"
}

approve_ref_review(){
	projectId=$1
        crId=$2
        curl -s --location --request POST "https://$api_domain/api/v4/projects/$projectId/code_reviews/$crId/submit_review_opinion" \
                --header "PRIVATE-TOKEN: $api_token" \
                --header "X-Tenant-Id: $tenant_id" \
                --header 'Cookie: login_aliyunid_csrf=_csrf_tk_1732312166325121' \
                --header 'Content-Type: application/json' \
                --data-raw "{\"review_opinion\": \"PASS\",\"review_comment\":\"\",\"submit_draft_comment_ids\":[],\"review_original_comment\": \"pass\"}"
}

function assert() {
    expected="$1"
    actual="$2"

    if [ "$expected" != "$actual" ]
    then
        echo "Assertion failed"
        exit 1
    fi
}